import request from "@/utils/request"
import shareInfo from "@/utils/shareInfo"
import { getShowImage, handleUrlParams } from "@/utils"
import store from "@/store"
import { userActionType } from "@/store/modules/user"

const common = {
  details: null,
  //获取课程班详情
  getDetails(id) {
    return new Promise((resolve, reject) => {
      request({
        url: "courseClass/getDetailsById",
        data: {
          id: id,
        },
        errorHandle: true,
        showLoading: true,
      }).then((data) => {
        this.details = data
        resolve(data)
      })
    })
  },
  //生成课程班分享参数
  async getShareInfo(shareType = "", shareParams = {}) {
    return {
      title: this.details.shareTitle ? this.details.shareTitle : this.details.className, //取后台设置的分享标题
      path: handleUrlParams("/pages/course/detail", {
        id: this.details.id,
        shareType: shareType,
        shareUserId: (await store.dispatch(userActionType.action.userInfo)).id,
        ...shareParams
      }),
      imageUrl: getShowImage(this.details.imageUrl ? this.details.imageUrl : this.details.topCarouselList[0]), //取后台设置的分享图片
    }
  },
}

export default common
